package com.yun.util.yun.utils.date;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CommonsUtil {
    private static volatile int count = 0;
    //生成唯一主键用
    public static String getUniqueID() {
       /* if (count >9999) {
            count = 0;
        }*/
        /*DateFormat _nsf = new SimpleDateFormat("yyyyMMddHHmmssSSS");*/
        DecimalFormat df = new DecimalFormat("0000");
        /*System.out.println(formatDate(new Date(), _nsf));*/
        /*return formatDate(new Date(), _nsf) + df.format(count++);*/
        return df.format(count++);
    }

    public static Date toDate(String dateStr) {
        if (dateStr == null || dateStr.isEmpty()) {
            return null;
        }
        DateFormat _dtf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat _dmf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        DateFormat _ddf = new SimpleDateFormat("yyyy-MM-dd");

        DateFormat _ttf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        DateFormat _tmf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        DateFormat _tdf = new SimpleDateFormat("yyyy/MM/dd");

        DateFormat _ntf = new SimpleDateFormat("yyyyMMddHHmmss");
        DateFormat _nmf = new SimpleDateFormat("yyyyMMddHHmm");
        DateFormat _ndf = new SimpleDateFormat("yyyyMMdd");
        try {
            return _dtf.parse(dateStr);
        } catch (Exception e1) {
            try {
                return _dmf.parse(dateStr);
            } catch (Exception e2) {
                try {
                    return _ddf.parse(dateStr);
                } catch (Exception e3) {
                    try {
                        return _ttf.parse(dateStr);
                    } catch (Exception e4) {
                        try {
                            return _tmf.parse(dateStr);
                        } catch (Exception e5) {
                            try {
                                return _tdf.parse(dateStr);
                            } catch (Exception e6) {
                                try {
                                    return _ntf.parse(dateStr);
                                } catch (Exception e7) {
                                    try {
                                        return _nmf.parse(dateStr);
                                    } catch (Exception e8) {
                                        try {
                                            return _ndf.parse(dateStr);
                                        } catch (Exception e9) {
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return null;
    }

    public static String formatDate(Date time) {
        DateFormat _dtf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatDate(time, _dtf);
    }

    public static String formatDate(Date time, DateFormat dtf) {
        if (time == null) {
            return null;
        }
        try {
            return dtf.format(time);
        } catch (Exception e) {
        }
        return time.toString();
    }

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {

            System.out.println(getUniqueID());
        }
        long end = System.currentTimeMillis();
        System.out.println(end-start);
    }
}
